import * as types from "../types/updateInfoTypes";
import NetWorkUtil from "../utils/NetWorkUtil";
import ApiConstants from "../common/ApiConstants";

export function getUpdateStandard(facility_id) {
  return dispatch => {
    dispatch({ type: types.UPDATEINFO_STANDARD_INITING });
    NetWorkUtil.getWithoutLogin(ApiConstants.DEVICEMODEL_UPDATEDEVICESTANDARD, { facility_id })
      .then(res => {
        dispatch({ type: types.UPDATEINFO_STANDARD_DONE, updateStandard: res.list, curUpdateStandard: res.list[0] });
      })
      .catch(error => {
        dispatch({ type: types.UPDATEINFO_STANDARD_ERROR, error });
      });
  };
}

export function getUpdateDeviceList() {
  return dispatch => {
    dispatch({ type: types.UPDATEINFO_INITING });
    NetWorkUtil.getWithoutLogin(ApiConstants.DEVICEMODEL_UPDATEDEVICELIST, {})
      .then(res => {
        const updateModels = res.list;
        if (updateModels.length > 0) {
          dispatch(getUpdateStandard(updateModels[0].facility_id));
        }
        dispatch({ type: types.UPDATEINFO_DONE, updateModels });
      })
      .catch(error => {
        dispatch({ type: types.UPDATEINFO_ERROR, error });
      });
  };
}

export function updateStandardChange() {
  return dispatch => {
    dispatch({ type: types.UPDATEINFO_UPDATESTANDARDCHANGE });
  };
}

export function updateInfoUnmount() {
  return dispatch => {
    dispatch({ type: types.UPDATEINFO_UNMOUNT });
  };
}
